package com.bky.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.bky.config.CacheKeyEnum;
import com.bky.model.Add;
import com.bky.mq.base.BaseConsumer;
import com.bky.mq.constant.Constants;
import com.bky.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;

public class AddConsumer extends BaseConsumer<Add>{

    @Autowired
    private RedisService redisService;

    @Override
    protected String getExceptionMessage() {
        return "sync canal data to mongo error！";
    }

    @Override
    protected Add checkBusinessData(JSONObject jsonObject) {
        if (!jsonObject.containsKey(Constants.ADD_ID_KEY)){
            return null;
        }
        if (!jsonObject.containsKey(Constants.ADD_NAME_KEY)){
            return null;
        }
        if (!jsonObject.containsKey(Constants.ADD_PWD_KEY)){
            return null;
        }
        return jsonObject.toJavaObject(Add.class);
    }

    @Override
    protected void processBusiness(Add add) {
        String key = CacheKeyEnum.KEY_ADD.format(add.getId());
        redisService.setObject(key, add, CacheKeyEnum.KEY_ADD.getExp());
    }
}
